import 'package:bitcoin_mine/services/in_app_purchase_service.dart';
import 'package:bitcoin_mine/widgets/animated_subscribe_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:in_app_purchase/in_app_purchase.dart';
import '../l10n/app_localizations.dart';
import '../controllers/home_controller.dart';
import '../common/global.dart';
import '../model/mine_info.dart';

class BeginnerMiningSection extends StatelessWidget {
  const BeginnerMiningSection({super.key});

  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context);
    final HomeController controller = Get.find();
    final InAppPurchaseService inAppPurchaseService = Get.find();
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 20.w),
      height: 76.h,
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImage('assets/images/pic_select_price_bg.png'),
          fit: BoxFit.fill,
        ),
      ),
      child: Obx(() {
        final HomeController c = controller;
        final MineInfo? info = Global.mineInfo.value;
        final List<Good> goods = _goodsByTier(info, c.selectedTier.value);
        Good? selected;
        if (goods.isNotEmpty && c.selectedPlan.value < goods.length) {
          selected = goods[c.selectedPlan.value];
        }
        final good = Global.goods.firstWhere(
          (element) => element.product_id == selected?.product_id,
        );
        final ProductDetails? productInfo = inAppPurchaseService.getProductInfo(
          good.product_id,
        );
        final String periodText = controller.periodText(selected?.circle ?? 1);
        final String priceText =
            '${good.discount_price != null ? '${productInfo?.currencySymbol}${good.discount_price}' : '${productInfo?.price}'}/$periodText';
        final String nextText = 'The next: ${productInfo?.price}/$periodText';
        // final String speedText = '${good.hashrate.toStringAsFixed(2)} Gh/s';
        // final String detailsText = '+ Continuous improvement\nMining speed';
        // final String? promo =
        //     good.promotional.isNotEmpty ? "${good.promotional}%" : null;
        // final String priceText =
        //     selected != null
        //         ? '\$${selected.price.toStringAsFixed(2)}/${controller.periodText(selected.circle)}'
        //         : controller.currentSelectedPrice;

        return Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            // 左侧信息
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    l10n.beginnerMining,
                    style: Theme.of(context).textTheme.bodyMedium?.copyWith(
                      color: const Color(0xFFFFFFFF),
                      fontSize: 16.sp,
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                  SizedBox(height: 2.h),
                  Text(
                    priceText,
                    style: Theme.of(context).textTheme.headlineSmall?.copyWith(
                      color: const Color(0xFFFFFFFF),
                      fontSize: 22.sp,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  Text(
                    nextText,
                    style: Theme.of(context).textTheme.headlineSmall?.copyWith(
                      color: const Color(0xFFFFFFFF),
                      fontSize: 12.sp,
                      fontWeight: FontWeight.normal,
                    ),
                  ),
                ],
              ),
            ),
            SizedBox(width: 10.w),
            // 订阅按钮（带动画效果）
            AnimatedSubscribeButton(
              text: l10n.subscribe,
              onTap: () {
                inAppPurchaseService.purchaseProduct(
                  controller.getCurrentProduct(),
                );
              },
              textStyle: Theme.of(context).textTheme.bodyMedium?.copyWith(
                color: const Color(0xFF061125),
                fontSize: 16.sp,
                fontWeight: FontWeight.w600,
              ),
            ),
          ],
        );
      }),
    );
  }
}

List<Good> _goodsByTier(MineInfo? info, int tier) {
  if (info == null) return [];
  switch (tier) {
    case 0:
      return info.goods_map.group1;
    case 1:
      return info.goods_map.group2;
    case 2:
      return info.goods_map.group3;
    case 3:
      return info.goods_map.group4;
    default:
      return info.goods_map.group1;
  }
}
